<?php

/**
 * uniBoard ACP Edit Forum
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_edit_forum{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Aviable forums
	 *
	 * @var array
	 */
	
	private $forums			= array();
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create API
		$this -> api = new api_forum( $appCore, $appCore -> _REQ('forum'));
		
		// Forum not found?
		if ( !$this -> api -> data_loaded)
		{
			// Draw Error
			$appCore -> output -> error( $appCore -> lang['acp_forum_not_found'], $appCore -> lang['acp_forums']);
			
			// Jump back to Forums List
			new action_forums( $appCore);
		}
		else if ( $appCore -> _REQ('do') == 'edit')
		{
			// Set First Forum
			$this -> forums[0] = $appCore -> lang['acp_forum_parent_0'];
			
			// Make new forums
			$this -> forums += $appCore -> getForums( true, false, $this -> api -> forum_id);
			
			// Update?
			if ( $appCore -> _POST( 'update') == 1)
			{
				// Rebuild perms?
				$refresh_acl = false;
				
				// New Pos?
				if ( $appCore -> _POST('forum_parent') != $this -> api -> forum_parent)
				{
					// Set Parent
					$this -> api -> setForumParent( $appCore -> _POST( 'forum_parent'));
				
					// Set positions
					$appCore -> db -> query( 'SELECT forum_pos
					FROM ' . DB_PREFIX . 'forums
					WHERE forum_parent = \'' . $this -> api -> forum_parent . '\'
					ORDER BY forum_pos
					LIMIT 1');
					
					// Found Pos?
					if ( $pos_result = $appCore -> db -> fetch_array())
					{
						// Set next highest
						$this -> api -> setForumPos($pos_result[0] + 1);	
					}
					
					// Free Result
					$appCore -> db -> freeResult();
					
					// Forum parent not rite?
					if ( !isset($this -> forums[$appCore -> _POST( 'forum_parent')]))
					{
						$this -> api -> setError($appCore -> lang['acp_forum_parent_incorrect']);
					}
					
					// Refresh ACL
					$refresh_acl = true;
				}
				
				// Fill Api
				$this -> api -> setForumName( $appCore -> _POST( 'forum_name'));
				$this -> api -> setForumDescriptionRaw( $appCore -> _POST( 'forum_description'));
				$this -> api -> setForumDescriptionSet( $appCore -> getByte( 'forum_description_bbcode') . $appCore -> getByte( 'forum_description_smileys') . $appCore -> getByte( 'forum_description_links'));
				$this -> api -> setForumType( $appCore -> _POST( 'forum_parent') == 0 ? 0 : $appCore -> _POST( 'forum_type'));
				$this -> api -> setForumUrl( $appCore -> _POST( 'forum_url'));
				$this -> api -> setForumRedirects( $appCore -> _POST( 'forum_redirects'));
				$this -> api -> setForumAllowPolls( $appCore -> _POST( 'forum_allow_polls'));
				$this -> api -> setForumAllowLinks( $appCore -> _POST( 'forum_allow_links'));
				$this -> api -> setForumAllowIcons( $appCore -> _POST( 'forum_allow_icons'));
				$this -> api -> setForumCountPosts( $appCore -> _POST( 'forum_count_posts'));
				$this -> api -> setForumDisplay( $appCore -> _POST( 'forum_display'));
				$this -> api -> setForumClosed( $appCore -> _POST( 'forum_closed'));
											
				// Api All-rite?
				if ( $this -> api -> error)
				{
					// Draw Errors
					$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> lang['acp_new_forum_form']);
					
					// Repeat Form
					$this -> drawForm();
				}
				else
				{	
					// Start Parser
					$parser = new class_parser($appCore, false);
					
					// Parse Text
					$this -> api -> setForumDescription( $parser -> preparse( $this -> api -> forum_description_raw, array(
						'bbcode' => $this -> api -> forum_description_set[0],
						'smileys' => $this -> api -> forum_description_set[1],
						'urls' => 1,
						'links' => $this -> api -> forum_description_set[2],
						'geshi' => 1,
						'images' => 1,
						'youtube' => 1
					)));
							
					// Save Api
					$this -> api -> saveData();
					
					// Copy Perms?
					if ( isset($this -> forums[$appCore -> _POST( 'forum_copy_perms')]) && $appCore -> _POST( 'forum_copy_perms') > 0)
					{
						// Source forum?
						$copy_id = (int) $appCore -> _POST( 'forum_copy_perms');
						
						// Select Masks
						$appCore -> db -> query( 'SELECT mask_id, mask_permissions FROM ' . DB_PREFIX . 'permission_masks');
						
						// Copy Perms
						while ( $mask_result = $appCore -> db -> fetch_array())
						{
							// Unserialise perms
							$mask_perms = (array) unserialize( $mask_result[1]);
							
							// Forum Show
							if ( in_array( $copy_id, $mask_perms['forum_show']))
							{
								$mask_perms['forum_show'][] = $this -> api -> forum_id;
							}
							
							// Forum Read
							if ( in_array( $copy_id, $mask_perms['forum_read']))
							{
								$mask_perms['forum_read'][] = $this -> api -> forum_id;
							}
							
							// Topics Start
							if ( in_array( $copy_id, $mask_perms['topics_start']))
							{
								$mask_perms['topics_start'][] = $this -> api -> forum_id;
							}
							
							// Topics Reply
							if ( in_array( $copy_id, $mask_perms['topics_reply']))
							{
								$mask_perms['topics_reply'][] = $this -> api -> forum_id;
							}
							
							// Files Upload
							if ( in_array( $copy_id, $mask_perms['files_upload']))
							{
								$mask_perms['files_upload'][] = $this -> api -> forum_id;
							}
							
							// Files Download
							if ( in_array( $copy_id, $mask_perms['files_download']))
							{
								$mask_perms['files_download'][] = $this -> api -> forum_id;
							}
							
							// Update Mask
							$appCore -> db -> update( array( 'mask_permissions' => $appCore -> db -> strEscape( serialize( $mask_perms))), 'permission_masks', 'mask_id = \'' . $mask_result[0] . '\'');
						}
						
						// Free Result
						$appCore -> db -> freeResult();
						
						// Refresh ACL
						$refresh_acl = true;
					}
					
					// Prune perms?
					if ($refresh_acl)
					{
						// Prune Perms
						$appCore -> db -> update( array( 'user_acl_cache' => ''), 'users');						
					}
					
					// Set Log
					$appCore -> logAdmin( 'log_admin_edited_forum', htmlspecialchars( $this -> api -> forum_name));
					
					// Redirect us back to list
					$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=forums'), $appCore -> lang['acp_forum_edited']);
				}
			}
			else
			{
				// Draw Form
				$this -> drawForm();
			}
		}
		else
		{
			// Draw Error
			$appCore -> output -> error( $appCore -> lang['wrong_action_code'], $appCore -> lang['acp_forums']);
			
			// Jump back to Forums List
			new action_forums( $appCore);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_forum&do=edit&forum=' . $this -> api -> forum_id));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_forum_form_sect_display']);
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_forum_form_parent'], $this-> appCore -> drawList( 'forum_parent', $this -> forums, $this -> api -> forum_parent));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_forum_form_name'], $this-> appCore -> drawTextInput( 'forum_name', $this -> api -> forum_name));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_forum_form_description'], $this-> appCore -> drawTextArea( 'forum_description', $this -> api -> forum_description_raw) . $this -> appCore -> output -> parsingOps( 'forum_description', true, true, true, $this -> api -> forum_description_set[0], $this -> api -> forum_description_set[1], $this -> api -> forum_description_set[2]));
		
		// Fix Perms list
		$this -> forums[0] = $this -> appCore -> lang['acp_forum_form_copy_perms_0'];
		
		// Copy perms list
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_forum_form_copy_perms'], $this-> appCore -> drawList( 'forum_copy_perms', $this -> forums, $this -> appCore -> _POST('forum_copy_perms')), $this -> appCore -> lang['acp_forum_form_copy_perms_expl']);
		
		// Some other fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_forum_form_type'], $this-> appCore -> drawList( 'forum_type', array(
			0 => $this -> appCore -> lang['acp_forum_form_type_0'],
			1 => $this -> appCore -> lang['acp_forum_form_type_1'],
			2 => $this -> appCore -> lang['acp_forum_form_type_2']
		), $this -> api -> forum_type), $this -> appCore -> lang['acp_forum_form_type_expl']);
				
		// New Section
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_forum_form_sect_redirect']);
		
		// Link
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_forum_form_url'], $this-> appCore -> drawTextInput( 'forum_url', $this -> api -> forum_url), $this -> appCore -> lang['acp_forum_form_url_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_forum_form_redirects'], $this-> appCore -> drawTextInput( 'forum_redirects', $this -> api -> forum_redirects));
				
		// New Section
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_forum_form_sect_general']);
		
		// Draw Fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_forum_form_allow_polls'], $this-> appCore -> drawYesNo( 'forum_allow_polls', $this -> api -> forum_allow_polls));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_forum_form_allow_links'], $this-> appCore -> drawYesNo( 'forum_allow_links', $this -> api -> forum_allow_links));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_forum_form_allow_icons'], $this-> appCore -> drawYesNo( 'forum_allow_icons', $this -> api -> forum_allow_icons));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_forum_form_count_posts'], $this-> appCore -> drawYesNo( 'forum_count_posts', $this -> api -> forum_count_posts));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_forum_form_display'], $this-> appCore -> drawYesNo( 'forum_display', $this -> api -> forum_display), $this -> appCore -> lang['acp_forum_form_display_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_forum_form_closed'], $this-> appCore -> drawYesNo( 'forum_closed', $this -> api -> forum_closed));
				
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( htmlspecialchars( $this -> api -> forum_name), array(
			$this -> appCore -> adminLink( 'act=forums') => $this -> appCore -> lang['acp_forums'],
			'' => htmlspecialchars( $this -> api -> forum_name)
		), ob_get_clean());
			
	}

}